package chen.jia.xin.finalproject.MainView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import chen.jia.xin.finalproject.R;

public class AllItemAdapter extends ArrayAdapter<AllItem> {
    //listview子项布局XML文件
    private  int resouceID;
    private  List<AllItem> objects;
    private Callback callback;
    //判断item中的按钮是否点击,解决ListView复用问题
    public static HashMap<Integer,Boolean> isSelect;
    private ViewHolder viewHolder;

    public AllItemAdapter(Context context, int resource,List<AllItem> objects,Callback callback) {
        super(context, resource, objects);
        this.objects = objects;
        this.resouceID = resource;
        this.callback = callback;
        isSelect = new HashMap<Integer,Boolean>();
        initData();
    }
    private void initData() {
        for(int i = 0;i< objects.size();i++){
            isSelect.put(i,false);
        }
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        //获取每一个子项
        AllItem eachItem = getItem(position);
        //缓存的View，所用控件的实例都缓存在ViewHolder中，就不用每次通过find获取控件实例
        viewHolder = null;
        //重用的View(将之前加载好的布局缓存好)，用它来间接获取viewHolder
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resouceID,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = view.findViewById(R.id.all_image);
            viewHolder.textView = view.findViewById(R.id.item_name);
            viewHolder.chatButton = view.findViewById(R.id.main_chat);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.imageView.setImageResource(eachItem.getImageID());
        viewHolder.textView.setText(eachItem.getImageName());

        //设置点击事件
        viewHolder.chatButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                if (isSelect.get(position)) {
                    viewHolder.chatButton.setText("特别关心");
                    isSelect.put(position, false);
                    //绑定点击和Tag
                    viewHolder.chatButton.setTag(R.id.btn,false);
                    callback.click(viewHolder);
                    notifyDataSetChanged();
                }
                else {
                    viewHolder.chatButton.setText("normal");
                    isSelect.put(position, true);
                    //绑定点击和Tag
                    viewHolder.chatButton.setTag(R.id.btn,true);
                    callback.click(viewHolder);
                    notifyDataSetChanged();
                }
            }
        });
        if(isSelect.get(position)) {
            viewHolder.chatButton.setText("特别关心");
            viewHolder.chatButton.setBackgroundColor(R.color.colorPrimaryDark);
        }else{
            viewHolder.chatButton.setText("normal");
            viewHolder.chatButton.setBackgroundColor(R.color.item_back);
        }
        return view;
    }

    //设置一个回调接口，可以在Activity中调用(以后可以用)
    public interface Callback {
        void click(ViewHolder viewHolder);
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView;
        Button chatButton;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public AllItem getItem(int position) {
        return objects.get(position);
    }

}
